package com.rocker.graduation.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import org.springframework.web.util.WebUtils;

import com.rocker.graduation.constants.Constants;
import com.rocker.graduation.entity.CartBean;
import com.rocker.graduation.entity.ConsigneeBean;
import com.rocker.graduation.entity.OrderBean;
import com.rocker.graduation.service.ICartService;
import com.rocker.graduation.service.IOrderService;

@Controller
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private IOrderService ordersService;
	
	@Autowired
	private ICartService cartService;
	
	@RequestMapping("/listOrders.action")
	public ModelAndView listOrders(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		Integer userId = (Integer) WebUtils.getSessionAttribute(request, Constants.USER_ID);
		if (null == userId || userId == 0) {
			mav.setViewName("redirect:/user/facade.action");
			return mav;
		}
		List<OrderBean> orderList = ordersService.listOrders(userId);
		Integer count = cartService.getItemSummary(userId);
		mav.addObject("itemCount", count);
		mav.setViewName("/account");
		mav.addObject("orders", orderList);
		return mav;
	}

	@RequestMapping("/cancelOrder.action/{orderId}")
	public ModelAndView cancelOrder(HttpServletRequest request, @PathVariable("orderId") String orderId){
		ModelAndView mav = new ModelAndView();
		MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
		Map<String, Object> attributes = new HashMap<String, Object>();
		int count = ordersService.cancelOrder(orderId);
		if (count > 0) {
			attributes.put(Constants.SUCCESS, Constants.SUCCESS);
		}else {
			attributes.put(Constants.SUCCESS, Constants.FAILED);
		}
		jsonView.setAttributesMap(attributes);
		mav.setView(jsonView);
		return mav;
	}
	
	@RequestMapping("/saveConsignee.action")
	public ModelAndView saveConsignee(HttpServletRequest request, @RequestBody ConsigneeBean consignee){
		ModelAndView mav = new ModelAndView();
		MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
		Map<String, Object> attributes = new HashMap<String, Object>();
		Integer userId = (Integer) WebUtils.getSessionAttribute(request, Constants.USER_ID);
		if (null == userId) {
			attributes.put(Constants.ERROR, Constants.ERROR_NULL_USER);
		}else {
			int type = ordersService.checkConsignee(userId);
			boolean isSuccess = ordersService.saveConsignee(userId, type, consignee);
			
			if (isSuccess) {
				attributes.put(Constants.ERROR, Constants.SUCCESS);
			}else {
				attributes.put(Constants.ERROR, Constants.FAILED);
			}
		}
		
		jsonView.setAttributesMap(attributes);
		mav.setView(jsonView);
		return mav;
	}
	
	@RequestMapping("/getSuccessOrder.action")
	public ModelAndView getSuccessOrder(@RequestParam("orderid") String orderId){
		ModelAndView mav = new ModelAndView();
		mav.addObject(Constants.URL, Constants.URL_PAYMENT + orderId);
		mav.addObject("type", "payment");
		mav.setViewName("/success");
		return mav;
	}
	
	@RequestMapping("/convertOrder.action")
	public ModelAndView convertOrder(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		Integer userId = (Integer) WebUtils.getSessionAttribute(request, Constants.USER_ID);
		if (null == userId) {
			mav.setViewName("redirect:/user/facade.action");
			return mav;
		}
		ConsigneeBean consignee = ordersService.getConsignee(userId);
		List<CartBean> cartBeans = cartService.getCartItem(userId);
		mav.addObject("items", cartBeans);
		mav.addObject("consignee", consignee);
		mav.setViewName("/payment");
		return mav;
	}
	
	@RequestMapping("/saveOrder.action")
	public ModelAndView saveOrder(HttpServletRequest request, @RequestParam("payType") String payType, 
			@RequestParam("shipType") String shipType){
		ModelAndView mav = new ModelAndView();
		Integer userId = (Integer) WebUtils.getSessionAttribute(request, Constants.USER_ID);
		if (null == userId || userId == 0) {
			mav.setViewName("redirect:/user/facade.action");
			return mav;
		}
		MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
		Map<String, Object> attributes = new HashMap<String, Object>();
		String orderId = ordersService.saveOrder(userId, payType, shipType);
		if (!StringUtils.isEmpty(orderId)) {
			attributes.put(Constants.ERROR, Constants.SUCCESS);
			attributes.put(Constants.ORDER_ID, orderId);
		}else {
			attributes.put(Constants.ERROR, Constants.FAILED);
		}
		jsonView.setAttributesMap(attributes);
		mav.setView(jsonView);
		return mav;
	}
	
	@RequestMapping("/payForOrder.action")
	public ModelAndView pay4Order(@RequestParam("orderId") String orderId){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pay");
		mav.addObject(Constants.ORDER_ID, orderId);
		
		return mav;
	}
	
	@RequestMapping("/pay.action")
	public ModelAndView pay(@RequestParam("orderId") String orderId){
		ModelAndView mav = new ModelAndView();
		MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
		Map<String, Object> attributes = new HashMap<String, Object>();
		boolean isSuccess = ordersService.pay(orderId);
		if (isSuccess) {
			attributes.put(Constants.ERROR, Constants.SUCCESS);
		}else {
			attributes.put(Constants.ERROR, Constants.FAILED);
		}
		jsonView.setAttributesMap(attributes);
		mav.setView(jsonView);
		return mav;
	}
	
	@RequestMapping("/item.action")
	public ModelAndView getOrderItem(HttpServletRequest request, @RequestParam String orderId){
		ModelAndView mav = new ModelAndView();
		//MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
		//Map<String, Object> attributes = new HashMap<String, Object>();
		Integer userId = (Integer) WebUtils.getSessionAttribute(request, Constants.USER_ID);
		if (null == userId || userId == 0) {
			mav.setViewName("redirect:/user/facade.action");
			return mav;
		}
		OrderBean orderBean = ordersService.getOrderItem(orderId, userId);
		//attributes.put(Constants.ERROR, Constants.SUCCESS);
		//jsonView.setAttributesMap(attributes);
		//mav.setView(jsonView);
		Integer count = cartService.getItemSummary(userId);
		mav.addObject("itemCount", count);
		mav.addObject("order", orderBean);
		mav.setViewName("/order");
		return mav;
	}
}
